package com.sbox.util;

import java.util.Date;

import com.sbox.model.FilterPeriod;
import com.sbox.model.NoteItem;
import com.sbox.model.SBoxConst;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

public class Utils {
	
	public static String getTimeString(long time)
	{
		Date date = new Date(time);
		
		return date.toLocaleString();
	}
	
	public static void closeInputMethod(Context c, View v)
	{
		InputMethodManager imm = (InputMethodManager)c.getSystemService(Context.INPUT_METHOD_SERVICE);
		
		if( imm != null )
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
	
	public static int getDisplayWidth(Context c)
	{
		WindowManager ws = (WindowManager)c.getSystemService(Context.WINDOW_SERVICE);
		Display display = ws.getDefaultDisplay();
		
		return display.getWidth();
	}
	
	public static int getDisplayHeight(Context c)
	{
		WindowManager ws = (WindowManager)c.getSystemService(Context.WINDOW_SERVICE);
		Display display = ws.getDefaultDisplay();
		
		return display.getHeight();
	}
	
	public static boolean isNoteInPeriod(long now, long time, int period)
	{
		long diff = (now-time)/1000;
		
		SBoxLog.info("now: " + now + ", ID: " + time + ", diff = " + diff);
		switch( period )
		{
		case FilterPeriod.PERIOD_ALL:
			return true;
		case FilterPeriod.PERIOD_1MONTH:
			if( diff > SBoxConst.PERIOD_1MONTH )
				return false;
			else
				return true;
		case FilterPeriod.PERIOD_3MONTH:
			if( diff > SBoxConst.PERIOD_3MONTH )
				return false;
			else
				return true;
		case FilterPeriod.PERIOD_1YEAR:
			if( diff > SBoxConst.PERIOD_1YEAR )
				return false;
			else
				return true;
		}
		
		return true;
	}
	
	public static String getTitle(String text)
	{
		String title = text;
		int len = text.length();
		
		for( int i=0; i<len; i++ )
		{
			if( text.charAt(i) == '\n' )
			{
				title = text.substring(0, i);
				
				/*
				if( title.length() > 10 )
				{
					title = title.substring(0, 10) + "...";
				}
				*/
				break;
			}
		}
		
		return title;
	}
	
	
	private static String sSeed = null;
	public static String enctypt(Context c, String msg)
	{
		String strEnc = msg;
		
		if( sSeed == null )
		{
			TelephonyManager tm = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
			sSeed = tm.getSimSerialNumber();
			
			SBoxLog.info("seed=" + sSeed);
		}
		
		try
		{
			strEnc = SimpleCrypto.encrypt(sSeed, msg);
		}
		catch(Throwable e)
		{
			strEnc = msg;
			SBoxLog.error(e.getMessage());
		}
		
		return strEnc;
	}
	
	public static String decrypt(Context c, String msg)
	{
		String strDec = msg;
		
		if( sSeed == null )
		{
			TelephonyManager tm = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
			sSeed = tm.getSimSerialNumber();
			
			SBoxLog.info("seed=" + sSeed);
		}
		
		try
		{
			strDec = SimpleCrypto.decrypt(sSeed, msg);
		}
		catch(Throwable e)
		{
			strDec = msg;
			SBoxLog.error(e.getMessage());
		}
		
		return strDec;
	}
	
	public static void sleep(long time) {
		android.os.SystemClock.sleep(time);
	}
	
	public static String formatTime( long time ) {
		Date dt = new Date(time);
		return dt.toLocaleString();
	}
}
